#pragma once

#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>

namespace ThreadModule
{
    static int number = 1;
    using func_t = std::function<void(std::string name)>;

    //枚举类(C++11)：1.枚举值不会隐式转换成整数；2.有作用域保护
    enum class TSTATUS
    {
        THREAD_NEW,
        THREAD_RUNNING,
        THREAD_STOP
    };

    class Thread
    {
    private:
        //static成员函数不属于任何一个具体的类对象，它属于整个类本身，因此参数里就没有了指向特定对象的this指针
        static void* Routine(void* args)
        {
            //因为这里没有了this指针，没法直接访问类内的成员变量_func，所以需要把this指针当作参数传进来
            Thread* t = static_cast<Thread* >(args); //static_cast的作用：在编译时告诉编译器把args的类型转换成Thread*类型
            t->_status = TSTATUS::THREAD_RUNNING;
            t->_func(t->Name());
            return nullptr;
        }

        void EnableDetach()
        {
            _isJoinable = false;
        }
    public:
        Thread(func_t func):_func(func),_status(TSTATUS::THREAD_NEW),_isJoinable(true)
        {
            _name = "thread-" + std::to_string(number++);
            _pid = getpid();
        }
        //创建线程
        bool Start()
        {
            //如果处在已经开始的状态时就不能重新开始了
            if(_status != TSTATUS::THREAD_RUNNING)
            {
                int n = ::pthread_create(&_tid, nullptr, Routine, this);
                if(n != 0)
                {
                    std::cerr << "start error" << std::endl;
                    return false;
                }
                return true;
            }

            return false;
        }
        //停止线程
        bool Stop()
        {
            if(_status == TSTATUS::THREAD_RUNNING)
            {
                int n = ::pthread_cancel(_tid);
                if(n != 0)
                {
                    std::cerr << "cancel thread error" << std::endl;
                    return false;
                }
                _status = TSTATUS::THREAD_STOP;
                return true;
            }
            return false;
        }
        //等待回收线程
        bool Join()
        {
            if(_isJoinable == true)
            {
                int n = ::pthread_join(_tid, nullptr);
                if(n != 0)
                {
                    std::cerr << "join error" << std::endl;
                    return false;
                }
                _status = TSTATUS::THREAD_STOP;
                return true;
            }
            return false;
        }
        bool isJoinable()
        {
            return _isJoinable;
        }
        
        //分离线程
        bool Detach()
        {
            EnableDetach();
            int n = ::pthread_detach(_tid);
            if(n != 0)
            {
                std::cerr << "detach thread error" << std::endl;
                return false;
            }
            return true;
        }
        std::string Name() {return _name;}

        ~Thread()
        {}
    private:
        std::string _name;
        pthread_t _tid;
        pid_t _pid;
        func_t _func; //线程执行的函数
        bool _isJoinable; //是否分离线程，默认不分离
        TSTATUS _status;
    };
}